﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Caliburn.Core.Metadata;
using Caliburn.PresentationFramework.ApplicationModel;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
using Microsoft.Practices.ServiceLocation;

namespace Luna.WPF.ApplicationFramework
{
    public class CaliburnView
    {
        public static int GetMenuModel(DependencyObject obj)
        {
            return (int)obj.GetValue(MenuModelProperty);
        }

        public static void SetMenuModel(DependencyObject obj, int value)
        {
            obj.SetValue(MenuModelProperty, value);
        }

        public static readonly DependencyProperty MenuModelProperty =
            DependencyProperty.RegisterAttached("MenuModel", typeof(int), typeof(CaliburnView),
            new UIPropertyMetadata((o, a) =>
            {


            }));

    }

    public interface IMenuPresenter
    {
        void Add(object model);

        void Add(object model, object view);

        DependencyObject Menu { get; }

        void Attach(DependencyObject dependencyObject);

    }

    [Singleton(typeof(MenuPresenter))]
    public class ApplicationMenuPresenter : MenuPresenter<ApplicationMenu>
    {
        public override void Add(object model)
        {
            //Menu.Items
            base.Add(model);
        }
    }

    public class MenuPresenter<T> : MenuPresenter where T : DependencyObject
    {
        protected T Menu
        {
            get { return (T)base.Menu; }
        }
    }

    public class MenuPresenter : Presenter, IMenuPresenter
    {
        private static readonly IViewStrategy _viewStrategy;
        private static readonly IBinder _binder;

        static MenuPresenter() {
            _viewStrategy = ServiceLocator.Current.GetInstance<IViewStrategy>();
            _binder = ServiceLocator.Current.GetInstance<IBinder>();
        }

        public virtual void Add(object model)
        {
            Add(model, null);
        }

        public virtual void Add(object model, object view)
        {
            if (view == null)
                view = _viewStrategy.GetView(model, null, null);

            _binder.Bind(model, view, null);
        }

        private DependencyObject _menu;
        protected DependencyObject Menu { get { return _menu; } }

        DependencyObject IMenuPresenter.Menu
        {
            get
            {
                return _menu;
            }
        }
        
        public void Attach(DependencyObject dependencyObject)
        {
            this._menu = dependencyObject;
        }
    }
}
